﻿using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System; 
using System.Diagnostics; 
using System.Text;
using System.Xml;
public class UnCompress : MonoBehaviour {
	[DllImport("AXpkLib")]   
	public static extern bool UnCompressFile(string lpClassName,string lpWindowName);  
	[DllImport("AXpkLib")]   
	private static extern bool CompressFile(string lpClassName,string lpWindowName);  
	
	[DllImport("AXpkLib",CharSet = CharSet.Ansi)]   
	private static extern void UnCompressFileToString(string infilename,[MarshalAs(UnmanagedType.LPStr)] StringBuilder s,int buffsize);
	[DllImport("AXpkLib",CharSet = CharSet.Ansi)]   
	private static extern void UnCompressFileToStream(string infilename,byte[] s,int buffsize,ref int outsize);
	
	public static void CompressFileToZip(string filename)
	{
		CompressFile(filename,filename+".zip");
		System.IO.File.Delete(filename);
	}
	
	static char[] mFileHeader;
	public static char[] fileHeader
	{
		get
		{
			if(mFileHeader == null)
			{
				mFileHeader = (@"file:///").ToCharArray();
			}
			
			return mFileHeader;
		}
	}
	public static  bool xmlExist(string xmlname)
	{
		xmlname = xmlname.TrimStart(fileHeader);
		return System.IO.File.Exists(xmlname+".zip")||System.IO.File.Exists(xmlname) ;
	}
	public static  XmlDocument GetXmlDoc(string xmlname, System.Text.Encoding ecode)
	{
		xmlname = xmlname.TrimStart(fileHeader);
		XmlDocument xmlDoc = new XmlDocument(); 
		if(System.IO.File.Exists(xmlname+".zip") )
		{
			int pout =0 ;
			byte[] buff = new byte[5000000];
			UnCompressFileToStream(xmlname+".zip",buff,buff.Length,ref pout);
 
			string outstring = ecode.GetString(buff);
			xmlDoc.LoadXml(outstring);
		}
		else
		{
			if(System.IO.File.Exists(xmlname))
			{
				xmlDoc.Load(xmlname);	
			}
			else
			{
				UnityEngine.Debug.LogError("没有找到文件或者文件的压缩文件"+xmlname);
			}
		}
		return xmlDoc;
	}
	
	public static string GetText(string xmlname, System.Text.Encoding ecode)
	{
		string outstring = "";
		
		if(xmlname.StartsWith(@"file:///"))
			xmlname = xmlname.TrimStart(fileHeader);
		string zipName = xmlname;
		if(!zipName.EndsWith(".zip"))
			zipName = xmlname + ".zip";
		if(System.IO.File.Exists(zipName) )
		{
			int pout =0 ;
			byte[] buff = new byte[5000000];
			UnCompressFileToStream(zipName,buff,buff.Length,ref pout);
 
			outstring = ecode.GetString(buff);
		}
		else
		{
			if(System.IO.File.Exists(xmlname))
			{
				outstring = System.IO.File.ReadAllText(xmlname, ecode);			
			}
			else
			{
				UnityEngine.Debug.LogError("没有找到文件或者文件的压缩文件"+xmlname);
			}
		}
		
		return outstring;
	}
		
	
	public static UnCompress inst;
	void Awake()
	{
		inst = this;
	}
	public int sBufferSize = 800000;
	public static StringBuilder sb;
	public static string UnCompressFile(string infilename)
	{
		if(sb==null)
		{
			sb = new StringBuilder(inst.sBufferSize);
		}
		UnCompressFileToString(infilename,sb ,sb.Capacity );
		return sb.ToString();
	}
	void Savefile(string ott)
	{
		System.IO.FileInfo my = new System.IO.FileInfo(@"c:\\aa.txt");
				System.IO.StreamWriter sw = my.CreateText();
				string oustr = ott;
				for(int i =0;i< oustr.Length;i++)
				{
					sw.Write(oustr[i]);
				}
				sw.Close();
	}
//	void OnGUI()
//	{
//		if(Application.isEditor)
//		{
//			if(GUI.Button(new Rect( 200,50,100,20),"ttt"))
//			{
//				
//				string  vv =GameResources.rootPath + "xml/41_Objects.xml.zip";
//				string ott = UnCompressFile(vv);
//				XmlDocument dc = new XmlDocument();
//				dc.LoadXml(ott);
//				XmlNodeList nl =	dc.GetElementsByTagName("info");
//				
//				Savefile(ott);
//				
//			}
//		}
//	}
}
